home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gshtx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  15.6 KB  |  613 lines

  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gshtx.c,v 1.2 2000/09/19 19:00:29 lpd Exp $ */
  20. /* Stand-alone halftone/transfer function related code */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gsutil.h"        /* for gs_next_ids */
  26. #include "gxfmap.h"
  27. #include "gzstate.h"
  28. #include "gzht.h"
  29. #include "gshtx.h"        /* must come after g*ht.h */
  30.  
  31. /*
  32.  * Procedure to free the set of components when a halftone is released.
  33.  */
  34. private void
  35. free_comps(
  36.           gs_memory_t * pmem,
  37.           void *pvht,
  38.           client_name_t cname
  39. )
  40. {
  41.     gs_ht *pht = (gs_ht *) pvht;
  42.  
  43.     gs_free_object(pmem, pht->params.ht_multiple.components, cname);
  44.     gs_free_object(pmem, pvht, cname);
  45. }
  46.  
  47. /*
  48.  * Stub transfer function, to be applied to components that are not provided
  49.  * with a transfer function.
  50.  */
  51. private float
  52. null_closure_transfer(
  53.              floatp val,
  54.              const gx_transfer_map * pmap_dummy,    /* NOTUSED */
  55.              const void *dummy    /* NOTUSED */
  56. )
  57. {
  58.     return val;
  59. }
  60.  
  61.  
  62. /*
  63.  * Build a gs_ht halftone structure.
  64.  */
  65. int
  66. gs_ht_build(
  67.            gs_ht ** ppht,
  68.            uint num_comps,
  69.            gs_memory_t * pmem
  70. )
  71. {
  72.     gs_ht *pht;
  73.     gs_ht_component *phtc;
  74.     int i;
  75.  
  76.     /* must have at least one component */
  77.     *ppht = 0;
  78.     if (num_comps == 0)
  79.     return_error(gs_error_rangecheck);
  80.  
  81.     /* allocate the halftone and the array of components */
  82.     rc_alloc_struct_1(pht,
  83.               gs_ht,
  84.               &st_gs_ht,
  85.               pmem,
  86.               return_error(gs_error_VMerror),
  87.               "gs_ht_build"
  88.     );
  89.     phtc = gs_alloc_struct_array(pmem,
  90.                  num_comps,
  91.                  gs_ht_component,
  92.                  &st_ht_comp_element,
  93.                  "gs_ht_build"
  94.     );
  95.     if (phtc == 0) {
  96.     gs_free_object(pmem, pht, "gs_ht_build");
  97.     return_error(gs_error_VMerror);
  98.     }
  99.     /* initialize the halftone */
  100.     pht->type = ht_type_multiple;
  101.     pht->rc.free = free_comps;
  102.     pht->params.ht_multiple.components = phtc;
  103.     pht->params.ht_multiple.num_comp = num_comps;
  104.  
  105.     for (i = 0; i < num_comps; i++) {
  106.     phtc[i].cname = gs_ht_separation_Default;
  107.     phtc[i].type = ht_type_none;
  108.     }
  109.  
  110.     *ppht = pht;
  111.  
  112.     return 0;
  113. }
  114.  
  115. /*
  116.  * Set a spot-function halftone component in a gs_ht halftone.
  117.  */
  118. int
  119. gs_ht_set_spot_comp(
  120.                gs_ht * pht,
  121.                int comp,
  122.                gs_ht_separation_name sepname,
  123.                floatp freq,
  124.                floatp angle,
  125.                float (*spot_func) (floatp, floatp),
  126.                bool accurate,
  127.                gs_ht_transfer_proc transfer,
  128.                const void *client_data
  129. )
  130. {
  131.     gs_ht_component *phtc = &(pht->params.ht_multiple.components[comp]);
  132.  
  133.     if (comp >= pht->params.ht_multiple.num_comp)
  134.     return_error(gs_error_rangecheck);
  135.     if (phtc->type != ht_type_none)
  136.     return_error(gs_error_invalidaccess);
  137.  
  138.     phtc->cname = (pht->params.ht_multiple.num_comp == 1
  139.            ? gs_ht_separation_Default
  140.            : sepname);
  141.     phtc->type = ht_type_spot;
  142.     phtc->params.ht_spot.screen.frequency = freq;
  143.     phtc->params.ht_spot.screen.angle = angle;
  144.     phtc->params.ht_spot.screen.spot_function = spot_func;
  145.     phtc->params.ht_spot.accurate_screens = accurate;
  146.     phtc->params.ht_spot.transfer = gs_mapped_transfer;
  147.  
  148.     phtc->params.ht_spot.transfer_closure.proc =
  149.     (transfer == 0 ? null_closure_transfer : transfer);
  150.     phtc->params.ht_spot.transfer_closure.data = client_data;
  151.  
  152.     return 0;
  153. }
  154.  
  155. /*
  156.  * Set a threshold halftone component in a gs_ht halftone. Note that the
  157.  * caller is responsible for releasing the threshold data.
  158.  */
  159. int
  160. gs_ht_set_threshold_comp(
  161.                 gs_ht * pht,
  162.                 int comp,
  163.                 gs_ht_separation_name sepname,
  164.                 int width,
  165.                 int height,
  166.                 const gs_const_string * thresholds,
  167.                 gs_ht_transfer_proc transfer,
  168.                 const void *client_data
  169. )
  170. {
  171.     gs_ht_component *phtc = &(pht->params.ht_multiple.components[comp]);
  172.  
  173.     if (comp >= pht->params.ht_multiple.num_comp)
  174.     return_error(gs_error_rangecheck);
  175.     if (phtc->type != ht_type_none)
  176.     return_error(gs_error_invalidaccess);
  177.  
  178.     phtc->cname = (pht->params.ht_multiple.num_comp == 1
  179.            ? gs_ht_separation_Default
  180.            : sepname);
  181.     phtc->type = ht_type_threshold;
  182.     phtc->params.ht_threshold.width = width;
  183.     phtc->params.ht_threshold.height = height;
  184.     phtc->params.ht_threshold.thresholds = *thresholds;
  185.     phtc->params.ht_threshold.transfer = gs_mapped_transfer;
  186.  
  187.     phtc->params.ht_threshold.transfer_closure.proc =
  188.     (transfer == 0 ? null_closure_transfer : transfer);
  189.     phtc->params.ht_threshold.transfer_closure.data = client_data;
  190.  
  191.     return 0;
  192. }
  193.  
  194. /*
  195.  * Increase the reference count of a gs_ht structure by 1.
  196.  */
  197. void
  198. gs_ht_reference(
  199.            gs_ht * pht
  200. )
  201. {
  202.     rc_increment(pht);
  203. }
  204.  
  205. /*
  206.  * Decrement the reference count of a gs_ht structure by 1. Free the
  207.  * structure if the reference count reaches 0.
  208.  */
  209. void
  210. gs_ht_release(
  211.          gs_ht * pht
  212. )
  213. {
  214.     rc_decrement_only(pht, "gs_ht_release");
  215. }
  216.  
  217.  
  218. /*
  219.  *  Verify that a gs_ht halftone is legitimate.
  220.  */
  221. private int
  222. check_ht(
  223.         gs_ht * pht
  224. )
  225. {
  226.     int i;
  227.     bool have_default = false;
  228.     int num_comps = pht->params.ht_multiple.num_comp;
  229.  
  230.     if (pht->type != ht_type_multiple)
  231.     return_error(gs_error_unregistered);
  232.     for (i = 0; i < num_comps; i++) {
  233.     gs_ht_component *phtc = &(pht->params.ht_multiple.components[i]);
  234.  
  235.     if ((phtc->type != ht_type_spot) && (phtc->type != ht_type_threshold))
  236.         return_error(gs_error_unregistered);
  237.     if (phtc->cname == gs_ht_separation_Default) {
  238.         if (have_default)
  239.         return_error(gs_error_rangecheck);
  240.         else
  241.         have_default = true;
  242.     }
  243.     }
  244.     return have_default ? 0 : gs_error_rangecheck;
  245. }
  246.  
  247. /*
  248.  *  Load a transfer map from a gs_ht_transfer_proc function.
  249.  */
  250. private void
  251. build_transfer_map(
  252.               gs_ht_component * phtc,
  253.               gx_transfer_map * pmap
  254. )
  255. {
  256.     gs_ht_transfer_proc proc;
  257.     const void *client_info;
  258.     int i;
  259.     frac *values = pmap->values;
  260.  
  261.     if (phtc->type == ht_type_spot) {
  262.     proc = phtc->params.ht_spot.transfer_closure.proc;
  263.     client_info = phtc->params.ht_spot.transfer_closure.data;
  264.     } else {
  265.     proc = phtc->params.ht_threshold.transfer_closure.proc;
  266.     client_info = phtc->params.ht_threshold.transfer_closure.data;
  267.     }
  268.  
  269.     for (i = 0; i < transfer_map_size; i++) {
  270.     float fval =
  271.         proc(i * (1 / (double)(transfer_map_size - 1)), pmap, client_info);
  272.  
  273.     values[i] =
  274.         (fval <= 0.0 ? frac_0 : fval >= 1.0 ? frac_1 :
  275.          float2frac(fval));
  276.     }
  277. }
  278.  
  279. /*
  280.  *  Allocate the order and transfer maps required by a halftone, and perform
  281.  *  some elementary initialization. This will also build the component index
  282.  *  to order index map.
  283.  */
  284. private gx_ht_order_component *
  285. alloc_ht_order(
  286.           const gs_ht * pht,
  287.           gs_memory_t * pmem,
  288.           byte * comp2order
  289. )
  290. {
  291.     int num_comps = pht->params.ht_multiple.num_comp;
  292.     gx_ht_order_component *pocs = gs_alloc_struct_array(
  293.                                pmem,
  294.                        pht->params.ht_multiple.num_comp,
  295.                               gx_ht_order_component,
  296.                          &st_ht_order_component_element,
  297.                                "alloc_ht_order"
  298.     );
  299.     int inext = 1;
  300.     int i;
  301.  
  302.     if (pocs == 0)
  303.     return 0;
  304.     pocs->corder.transfer = 0;
  305.  
  306.     for (i = 0; i < num_comps; i++) {
  307.     gs_ht_component *phtc = &(pht->params.ht_multiple.components[i]);
  308.     gx_transfer_map *pmap = gs_alloc_struct(pmem,
  309.                         gx_transfer_map,
  310.                         &st_transfer_map,
  311.                         "alloc_ht_order"
  312.     );
  313.  
  314.     if (pmap == 0) {
  315.         int j;
  316.  
  317.         for (j = 0; j < inext; j++)
  318.         gs_free_object(pmem, pocs[j].corder.transfer, "alloc_ht_order");
  319.         gs_free_object(pmem, pocs, "alloc_ht_order");
  320.         return 0;
  321.     }
  322.     pmap->proc = gs_mapped_transfer;
  323.     pmap->id = gs_next_ids(1);
  324.     if (phtc->cname == gs_ht_separation_Default) {
  325.         pocs->corder.levels = 0;
  326.         pocs->corder.bit_data = 0;
  327.         pocs->corder.cache = 0;
  328.         pocs->corder.transfer = pmap;
  329.         pocs->cname = gs_ht_separation_Default;
  330.         comp2order[i] = 0;
  331.     } else {
  332.         pocs[inext].corder.levels = 0;
  333.         pocs[inext].corder.bit_data = 0;
  334.         pocs[inext].corder.cache = 0;
  335.         pocs[inext].corder.transfer = pmap;
  336.         pocs[inext].cname = phtc->cname;
  337.         comp2order[i] = inext++;
  338.     }
  339.     }
  340.  
  341.     return pocs;
  342. }
  343.  
  344. /*
  345.  *  Build the halftone order for one component.
  346.  */
  347. private int
  348. build_component(
  349.            gs_ht_component * phtc,
  350.            gx_ht_order * porder,
  351.            gs_state * pgs,
  352.            gs_memory_t * pmem
  353. )
  354. {
  355.     if (phtc->type == ht_type_spot) {
  356.     gs_screen_enum senum;
  357.     int code;
  358.  
  359.     code = gx_ht_process_screen_memory(&senum,
  360.                        pgs,
  361.                        &phtc->params.ht_spot.screen,
  362.                       phtc->params.ht_spot.accurate_screens,
  363.                        pmem
  364.         );
  365.     if (code < 0)
  366.         return code;
  367.  
  368.     /* avoid wiping out the transfer structure pointer */
  369.     senum.order.transfer = porder->transfer;
  370.     *porder = senum.order;
  371.  
  372.     } else {            /* ht_type_threshold */
  373.     int code;
  374.     gx_transfer_map *transfer = porder->transfer;
  375.  
  376.     porder->params.M = phtc->params.ht_threshold.width;
  377.     porder->params.N = 0;
  378.     porder->params.R = 1;
  379.     porder->params.M1 = phtc->params.ht_threshold.height;
  380.     porder->params.N1 = 0;
  381.     porder->params.R1 = 1;
  382.     code = gx_ht_alloc_threshold_order(porder,
  383.                        phtc->params.ht_threshold.width,
  384.                        phtc->params.ht_threshold.height,
  385.                        256,
  386.                        pmem
  387.         );
  388.     if (code < 0)
  389.         return code;
  390.     gx_ht_construct_threshold_order(
  391.                 porder,
  392.                 phtc->params.ht_threshold.thresholds.data
  393.         );
  394.     /*
  395.      * gx_ht_construct_threshold_order wipes out transfer map pointer,
  396.      * restore it here.
  397.      */
  398.     porder->transfer = transfer;
  399.     }
  400.  
  401.     build_transfer_map(phtc, porder->transfer);
  402.     return 0;
  403. }
  404.  
  405. /*
  406.  * Free an order array and all elements it points to.
  407.  */
  408. private void
  409. free_order_array(
  410.             gx_ht_order_component * pocs,
  411.             int num_comps,
  412.             gs_memory_t * pmem
  413. )
  414. {
  415.     int i;
  416.  
  417.     for (i = 0; i < num_comps; i++)
  418.     gx_ht_order_release(&(pocs[i].corder), pmem, true);
  419.     gs_free_object(pmem, pocs, "gs_ht_install");
  420. }
  421.  
  422.  
  423. /*
  424.  *  Install a gs_ht halftone as the current halftone in the graphic state.
  425.  */
  426. int
  427. gs_ht_install(
  428.          gs_state * pgs,
  429.          gs_ht * pht
  430. )
  431. {
  432.     int code = 0;
  433.     gs_memory_t *pmem = pht->rc.memory;
  434.     gx_device_halftone dev_ht;
  435.     gx_ht_order_component *pocs;
  436.     byte comp2order[32];    /* ample component to order map */
  437.     int num_comps = pht->params.ht_multiple.num_comp;
  438.     int i;
  439.  
  440.     /* perform so sanity checks (must have one default component) */
  441.     if ((code = check_ht(pht)) != 0)
  442.     return code;
  443.  
  444.     /* allocate the halftone order structure and transfer maps */
  445.     if ((pocs = alloc_ht_order(pht, pmem, comp2order)) == 0)
  446.     return_error(gs_error_VMerror);
  447.  
  448.     /* build all of the order for each component */
  449.     for (i = 0; i < num_comps; i++) {
  450.     int j = comp2order[i];
  451.  
  452.     code = build_component(&(pht->params.ht_multiple.components[i]),
  453.                    &(pocs[j].corder),
  454.                    pgs,
  455.                    pmem
  456.         );
  457.  
  458.     if ((code >= 0) && (j != 0)) {
  459.         gx_ht_cache *pcache;
  460.  
  461.         pcache = gx_ht_alloc_cache(pmem,
  462.                        4,
  463.                        pocs[j].corder.raster *
  464.                        (pocs[j].corder.num_bits /
  465.                     pocs[j].corder.width) * 4
  466.         );
  467.  
  468.         if (pcache == 0)
  469.         code = gs_note_error(gs_error_VMerror);
  470.         else {
  471.         pocs[j].corder.cache = pcache;
  472.         gx_ht_init_cache(pcache, &(pocs[j].corder));
  473.         }
  474.     }
  475.     if (code < 0)
  476.         break;
  477.     }
  478.  
  479.     if (code < 0) {
  480.     free_order_array(pocs, num_comps, pmem);
  481.     return code;
  482.     }
  483.     /* initialize the device halftone structure */
  484.     dev_ht.rc.memory = pmem;
  485.     dev_ht.order = pocs[0].corder;    /* Default */
  486.     if (num_comps == 1) {
  487.     /* we have only a Default; we don't need components. */
  488.     gs_free_object(pmem, pocs, "gs_ht_install");
  489.     dev_ht.components = 0;
  490.     } else {
  491.     dev_ht.components = pocs;
  492.     dev_ht.num_comp = num_comps;
  493.     }
  494.  
  495.     /* at last, actually install the halftone in the graphic state */
  496.     return gx_ht_install(pgs, (gs_halftone *) pht, &dev_ht);
  497. }
  498.  
  499. /* ---------------- Mask-defined halftones ---------------- */
  500.  
  501. /*
  502.  * Create a halftone order from an array of explicit masks.  This is
  503.  * silly, because the rendering machinery actually wants masks, but doing
  504.  * it right seems to require too many changes in existing code.
  505.  */
  506. private int
  507. create_mask_bits(const byte * mask1, const byte * mask2,
  508.          int width, int height, gx_ht_bit * bits)
  509. {
  510.     /*
  511.      * We do this with the slowest, simplest possible algorithm....
  512.      */
  513.     int width_bytes = (width + 7) >> 3;
  514.     int x, y;
  515.     int count = 0;
  516.  
  517.     for (y = 0; y < height; ++y)
  518.     for (x = 0; x < width; ++x) {
  519.         int offset = y * width_bytes + (x >> 3);
  520.         byte bit_mask = 0x80 >> (x & 7);
  521.  
  522.         if ((mask1[offset] ^ mask2[offset]) & bit_mask) {
  523.         if (bits)
  524.             gx_ht_construct_bit(&bits[count], width, y * width + x);
  525.         ++count;
  526.         }
  527.     }
  528.     return count;
  529. }
  530. private int
  531. create_mask_order(gx_ht_order * porder, gs_state * pgs,
  532.           const gs_client_order_halftone * phcop,
  533.           gs_memory_t * mem)
  534. {
  535.     int width_bytes = (phcop->width + 7) >> 3;
  536.     const byte *masks = (const byte *)phcop->client_data;
  537.     int bytes_per_mask = width_bytes * phcop->height;
  538.     const byte *prev_mask;
  539.     int num_levels = phcop->num_levels;
  540.     int num_bits = 0;
  541.     int i;
  542.     int code;
  543.  
  544.     /* Do a first pass to compute how many bits entries will be needed. */
  545.     for (prev_mask = masks, num_bits = 0, i = 0;
  546.      i < num_levels - 1;
  547.      ++i, prev_mask += bytes_per_mask
  548.     )
  549.     num_bits += create_mask_bits(prev_mask, prev_mask + bytes_per_mask,
  550.                      phcop->width, phcop->height, NULL);
  551.     code = gx_ht_alloc_client_order(porder, phcop->width, phcop->height,
  552.                     num_levels, num_bits, mem);
  553.     if (code < 0)
  554.     return code;
  555.     /* Fill in the bits and levels entries. */
  556.     for (prev_mask = masks, num_bits = 0, i = 0;
  557.      i < num_levels - 1;
  558.      ++i, prev_mask += bytes_per_mask
  559.     ) {
  560.     porder->levels[i] = num_bits;
  561.     num_bits += create_mask_bits(prev_mask, prev_mask + bytes_per_mask,
  562.                      phcop->width, phcop->height,
  563.                      ((gx_ht_bit *)porder->bit_data) +
  564.                       num_bits);
  565.     }
  566.     porder->levels[num_levels - 1] = num_bits;
  567.     return 0;
  568. }
  569.  
  570. /* Define the client-order halftone procedure structure. */
  571. private const gs_client_order_ht_procs_t mask_order_procs =
  572. {
  573.     create_mask_order
  574. };
  575.  
  576. /*
  577.  * Define a halftone by an explicit set of masks.  We translate these
  578.  * internally into a threshold array, since that's what the halftone
  579.  * rendering machinery knows how to deal with.
  580.  */
  581. int
  582. gs_ht_set_mask_comp(gs_ht * pht,
  583.             int component_index, gs_ht_separation_name sepr_name,
  584.             int width, int height, int num_levels,
  585.             const byte * masks,        /* width x height x num_levels bits */
  586.             gs_ht_transfer_proc transfer,
  587.             const void *client_data)
  588. {
  589.     gs_ht_component *phtc =
  590.     &(pht->params.ht_multiple.components[component_index]);
  591.  
  592.     if (component_index >= pht->params.ht_multiple.num_comp)
  593.     return_error(gs_error_rangecheck);
  594.     if (phtc->type != ht_type_none)
  595.     return_error(gs_error_invalidaccess);
  596.  
  597.     phtc->cname =
  598.     (pht->params.ht_multiple.num_comp == 1 ? gs_ht_separation_Default :
  599.      sepr_name);
  600.     phtc->type = ht_type_client_order;
  601.     phtc->params.client_order.width = width;
  602.     phtc->params.client_order.height = height;
  603.     phtc->params.client_order.num_levels = num_levels;
  604.     phtc->params.client_order.procs = &mask_order_procs;
  605.     phtc->params.client_order.client_data = masks;
  606.     phtc->params.client_order.transfer_closure.proc =
  607.     (transfer == 0 ? null_closure_transfer : transfer);
  608.     phtc->params.client_order.transfer_closure.data = client_data;
  609.  
  610.     return 0;
  611.  
  612. }
  613.